package com.dao.auth;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.common.base.BaseSqlMapDAO;

public class RoleResDao extends BaseSqlMapDAO{
	
	/**
	 * 查询所有URL的能访问的角色
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Map> getAllRolesUrls()throws Exception{
		
		return this.getSqlMapClientTemplate().queryForList("mroleres.getAllRolesUrls");
	}

	
	@SuppressWarnings("unchecked")
	public List<String> getAllResByRoleId(Integer roleId)throws Exception{
		
		return this.getSqlMapClientTemplate().queryForList("mroleres.getAllResByRoleId",roleId);
	} 
	
	
	
	/**
	 * 根据角色ID 删除此角色的所有资源
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	public boolean delAllResByRoleId(Integer roleId) throws Exception{
		
		return this.getSqlMapClientTemplate().delete("mroleres.delAllResByRoleId",roleId)>0;
	}
	
	
	/**
	 * 根据资源ID 删除角色资源关联关系
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	public boolean delAllResByResId(Integer resId) throws Exception{
		
		return this.getSqlMapClientTemplate().delete("mroleres.delAllResByResId",resId)>0;
	}
	
	
	/**
	 * 添加用户角色
	 * @param roleId
	 * @param resId
	 * @return
	 * @throws Exception
	 */
	public boolean insertRoleRes(Integer roleId,Integer resId) throws Exception{
		
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("roleId",roleId);
		map.put("resId",resId);
		
		return this.getSqlMapClientTemplate().insert("mroleres.insertRoleRes",map)!=null;
	}
}
